﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using CQCFrameWork.Tools.RewriteUIComponent.Editor;

public class LocalizationExtension : AddDefaultCanvas
{
    [MenuItem("GameObject/UI/Localization_Text", false)]
    static void AddText(MenuCommand menuCommand)
    {
        GameObject go = new GameObject("Text");
        LocalizationText txt = go.AddComponent<LocalizationText>();
        PlaceUIElementRoot(go, menuCommand);
    }

    /// <summary>
    /// 这里重写添加keystring属性必须要规范到serializedproperty keystring然后重写OnEnable，再在OnInspectorGUI里显示，
    /// 如果直接像我参考的那个人的写法会出现keystring的值修改失败（在预制体预览界面才会出现此bug）
    /// 用DelayedTextField是要按回车或者焦点移动才会完成，防止一直持续刷新增大开销
    /// 处理SerializedProperty类型的字段需要用以下两句包裹起来，否则会出问题，
    /// 详情参考UGUI源码：https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=downloads
    ///  serializedObject.Update();
    ///  serializedObject.ApplyModifiedProperties();
    /// </summary>
    [CustomEditor(typeof(LocalizationText), true)]
    class LocalizationTextEditor : UnityEditor.UI.TextEditor
    {
        private SerializedProperty KeyString;

        protected override void OnEnable()
        {
            base.OnEnable();
            KeyString = serializedObject.FindProperty("KeyString");
        }

        public override void OnInspectorGUI()
        {
            // 测了好多次不知道为什么 鼠标每点一次 这里面的代码就运行4次
            // Debug.Log(1);  每次都打印4个1
            LocalizationText component = (LocalizationText) target;
            base.OnInspectorGUI();
            serializedObject.Update();
            EditorGUILayout.ObjectField("Font Style", component.StyleFont(), typeof(UIFont), true);
            EditorGUILayout.DelayedTextField(KeyString);
            serializedObject.ApplyModifiedProperties();
            component.font = component.StyleFont().UseFont;
            string key = component.KeyString;
            if (!string.IsNullOrEmpty(key))
            {
                // 调用获取Localization.csv数据
                // 获取有几种语言
                // 判断当前的key有没有对应的value，有则显示Preview和赋值给Text文本框，没有则不显示Preview
                // 预览Preview框
                Localization.LoadDictionary();
                if (!Localization.dictionary.ContainsKey(key))
                {
                    return;
                }

                List<string> languages = Localization.dictionary["KEY"]; // 获取有几种语言
                List<string> value = Localization.dictionary[key];
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.black;
                style.fontSize = 20;
                style.alignment = TextAnchor.LowerLeft;
                GUILayout.BeginHorizontal();
                GUILayout.Toggle(true, "Preview", "dragtab", GUILayout.MinWidth(20f));
                GUILayout.EndHorizontal();
                for (int i = 0; i < value.Count; i++)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Box(languages[i]);
                    GUILayout.Space(15);
                    if (GUILayout.Button(value[i], style))
                    {
                        component.text = value[i];
                    }

                    GUILayout.EndHorizontal();
                }
            }
        }
    }
}

